package com.art.sunflower.service.impl;

import com.art.sunflower.exception.NullRsgrException;
import com.art.sunflower.mapper.RsgrMapper;
import com.art.sunflower.mapper.UserMapper;
import com.art.sunflower.service.RsgrService;
import com.art.sunflower.util.MyPageInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RsgrServiceImpl implements RsgrService {

    @Resource
    RsgrMapper rsgrMapper;

    @Resource
    UserMapper userMapper;

    @Override
    public void add(String title, String introduce, String startTime, String endTime, Integer[] ids) {
        rsgrMapper.insertRsgr(title, introduce, startTime, endTime);
        Integer rsgrId = userMapper.selectLastInsertId();
        for (Integer id : ids) {
            rsgrMapper.insertRsgrClasses(rsgrId, id);
        }
    }

    @Override
    public void delete(Integer id) throws NullRsgrException {
        Map<String, Object> rsgr = rsgrMapper.selectRsgr(id);
        if (rsgr == null) {
            throw new NullRsgrException();
        } else {
            rsgrMapper.delete(id);
        }
    }

    @Override
    public void update(Integer id, String title, String introduce, String startTime, String endTime, Integer[] ids) throws NullRsgrException {
        Map<String, Object> rsgr = rsgrMapper.selectRsgr(id);
        if (rsgr == null) {
            throw new NullRsgrException();
        } else {
            rsgrMapper.updateRsgr(id, title, introduce, startTime, endTime);
            rsgrMapper.deleteClasses(id);
            for (Integer classesId : ids) {
                rsgrMapper.insertRsgrClasses(id, classesId);
            }
        }
    }

    @Override
    public MyPageInfo getList(Integer pageSize, Integer pageNo) throws NullRsgrException {
        PageHelper.startPage(pageNo, pageSize);
        List<Map<String, Object>> rsgr = rsgrMapper.selectList();
        if (rsgr.size() == 0)
            throw new NullRsgrException();
        PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(rsgr);
        return new MyPageInfo(pageInfo);
    }

    @Override
    public Map<String, Object> getById(Integer id) throws NullRsgrException {
        Map<String, Object> rsgr = rsgrMapper.selectRsgr(id);
        if (rsgr == null) {
            throw new NullRsgrException();
        } else {
            List<Integer> classesId = rsgrMapper.selectClasserId(id);
            rsgr.put("classesId", classesId);
            return rsgr;
        }
    }

    @Override
    public Map<String, Object> getAllRsgr(Integer id) throws NullRsgrException {
        Map<String, Object> rsgr = rsgrMapper.selectRsgr(id);
        if (rsgr == null) {
            throw new NullRsgrException();
        } else {
            return rsgrMapper.selectAllRsgr(id);
        }
    }

}
